package demo.messaging.server;

import java.util.Map;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener {
	private final int sessionCount = 0;

	public void sessionCreated(final HttpSessionEvent event) {

	}

	public void sessionDestroyed(final HttpSessionEvent event) {
		Map<String, UserContext> map = (Map<String, UserContext>) event
				.getSession().getServletContext().getAttribute(
						ServerConstantes.USER_CONTEXT_MAP);

		if (map != null) {
			UserContext userToDelete = null;
			for (UserContext user : map.values()) {
				if (user.getSessionId() == event.getSession().getId()) {
					userToDelete = user;
				}
			}
			if (null != userToDelete) {
				map.remove(userToDelete.getUserName());
			}
		}
	}
}